<?php


/**
 * 附件Model
 * Enter description here ...
 * @author Admin
 *
 */
class AccessoryModel extends CommonModel {
	
	
	protected $_auto = array ( 
		array('uploadtime','time',1,'function'), // 对addtime字段在更新的时候写入当前时间戳
	);
	
	
	
	/**
	 * 获取附件记录
	 * @see CommonModel::getdata()
	 */
	public function getdata($map,$field='AC.*'){
		$prefix = C('DB_PREFIX');
		$data = $this->Table("{$prefix}accessory_relation as AR")->
					  join("{$prefix}accessory as AC ON AR.accessoryid = AC.id")->
					  field($field)->where($map)->
					  findAll();
		return $data;
	}
	

	/**
	 * 保存
	 * Enter description here ...
	 * @param unknown_type $data
	 */
	public function insert($data){
		$data = $this->create($data,1);
		if (empty($data) || $data === false){
			return false;
		}
		
		$info = $this->add($data);
		return $info;
	}
	
	
	
	
	/**
	 * 上传图片
	 * Enter description here ...
	 * @param unknown_type $thumbMaxWidth
	 * @param unknown_type $thumbMaxHeight
	 * @param unknown_type $origin
	 */
	public function imgUpload($thumbMaxWidth,$thumbMaxHeight,$origin){
		 	if(empty($thumbMaxWidth))
		 		$thumbMaxWidth='800,160';
			if(empty($thumbMaxHeight)
				)$thumbMaxHeight='600,120';
			if(empty($origin))
				$origin=false;
			
			import("@.ORG.UploadFile");
        	$upload = new UploadFile();
			$upload->allowExts = explode(',', 'jpg,gif,png,jpeg');
        	//设置附件上传目录
       		$upload->savePath = UPLOADIMG;
	            //设置上传文件大小
	        $upload->maxSize = 3292200;
	        //设置上传文件类型
	
	        //设置需要生成缩略图，仅对图像文件有效
	        $upload->thumb = true;
	        // 设置引用图片类库包路径
	        $upload->imageClassPath = '@.ORG.Image';
	        //设置需要生成缩略图的文件后缀
	        $upload->thumbPrefix = 'm_,s_';  //生产2张缩略图
	        //设置缩略图最大宽度
	        $upload->thumbMaxWidth = $thumbMaxWidth;
	        //设置缩略图最大高度
	        $upload->thumbMaxHeight = $thumbMaxHeight;
	        //设置上传文件规则
	        $upload->saveRule = uniqid;
	        //删除原图
	        $upload->thumbRemoveOrigin = $origin;
	        if (!$upload->upload()) {
	            //捕获上传异常
	            $this->error($upload->getErrorMsg());
	        } else {
	            //取得成功上传的文件信息
	            $uploadList = $upload->getUploadFileInfo();
	           // import("@.ORG.Image");
	            //给m_缩略图添加水印, Image::water('原文件名','水印图片地址')
	            //$path = $uploadList[0]['savepath'].'m_'.$uploadList[0]['savename'];
	            //Image::water($path, __PUBLIC__.'/images/watermark.png');
	        }
	        
	        
	        //保存当前数据对象
	        $data['path'] = 'm_'.$uploadList[0]['savename'];
	        $data['thumbnail'] = 's_'.$uploadList[0]['savename'];
	        $data['type'] = 'img';
	        $data['title']= '';
	        $data['size']= $uploadList[0]['size'];
	        $data['uploadtime'] = time();
	        
	        $list = $this->add($data);
	        
	       return $list;
	}
	
	
	
	
	/**
	 * 获取此晒图图片的用户
	 * Enter description here ...
	 * @param unknown_type $id
	 * @param unknown_type $uid
	 */
	public function isUser($id,$uid){
		$prefix = C('DB_PREFIX');
		$map['AC.id']=array('eq',$id);
		$map['AR.table']=array('eq','Sunpic');
		$map['S.uid']=array('eq',$uid);
		$data = $this->Table("{$prefix}accessory as AC")->
					  join("{$prefix}accessory_relation as AR ON AC.id = AR.accessoryid")->
					  join("{$prefix}sunpic as S ON S.id = AR.relationid")->
					  field('S.*')->where($map)->find();
		
		if (empty($data)){
			return false;
		}else {
			return true;
		}
	}
	
	
	/**
	 * 删除图片和关系
	 * Enter description here ...
	 * @param unknown_type $id
	 */
	public function delImg($id){
		$this->delete($id);
		
		$accessory_relation = M('Accessory_relation');
		$map['accessoryid']=array('eq',$id);
		$accessory_relation->where($map)->delete();
	}
	
	
	
	
}
?>